﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace 城市天然气管道风险管理与决策系统
{
    public partial class Form_menu_2 : Form
    {
        private readonly string[] txt1_1 = { "日常监控", "日常监控", "提高管道检测与巡线频率，加强管道防腐工作", "老旧管道尽量改线，若无法该线，加强监控与防护，新建管道杜绝此类情况发生", "老旧管道尽量改线，若无法该线，加强监控与防护，新建管道杜绝此类情况发生" };
        private readonly int[] Score = { 8, 6, 4, 2, 0 };
        private readonly string[] txt1_2 = {"日常监控","日常监控","加强监控，提高防腐层质量与厚度或采用其他防腐技术",
        "老旧管道尽量改线，若无法该线，加强监控与防护，新建管道杜绝此类情况发生","老旧管道尽量改线，若无法该线，加强监控与防护，新建管道杜绝此类情况发生"};
        private readonly string[] txt1_3 = { "日常监控", "日常监控", "加强监控，必要时提高防腐手段，如阴极防护等",
        "针对老旧管道需加强监控与防腐，新建管线要避免此类问题，提高防腐层厚度", "针对老旧管道需加强监控与防腐，新建管线要避免此类问题，提高防腐层厚度" };
        private readonly string[] txt1_4 = { "日常监控", "日常监控", "加强管内监控", "加强管内监控，内部注入质量合格的缓腐剂，输送前进行脱水和过滤分离，保证运行方式合理",
        "加强管内监控，内部注入质量合格的缓腐剂，输送前进行脱水和过滤分离，保证运行方式合理"};
        private readonly string[] txt1_5 = { "日常监控", "日常监控", "关注管件转弯处应力腐蚀监测", 
            "加强员工培训，降至安全操作压力运行；加强管件转弯处腐蚀检测；腐蚀严重者应立即修复", "加强员工培训，降至安全操作压力运行；加强管件转弯处腐蚀检测；腐蚀严重者应立即修复" };
        private readonly Color[] degree_color = { Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Red };
        private readonly string[] txt2_1 = { "日常监控", "日常监控", "加强管线监控，采取必要的维修措施提高管材质量", "加强管线监控，采取必要的维修措施提高管材质量", "加强管线监控，及时替换管线；新建管线投产前要做好设备质量检测" };
        private readonly string[] txt2_2 = { "日常监控", "日常监控", "投产前，提高水压试验压力，排除更多潜在焊缝缺陷；投产后加强管线监测投产前，提高水压试验压力，排除更多潜在焊缝缺陷，直至焊接合格再投产；投产后加强管线监测，提高焊缝腐蚀质量"
                ,"投产前，提高水压试验压力，排除更多潜在焊缝缺陷；投产后加强管线监测投产前，提高水压试验压力，排除更多潜在焊缝缺陷，直至焊接合格再投产；投产后加强管线监测，提高焊缝腐蚀质量"
                ,"投产前，提高水压试验压力，排除更多潜在焊缝缺陷；投产后加强管线监测投产前，提高水压试验压力，排除更多潜在焊缝缺陷，直至焊接合格再投产；投产后加强管线监测，提高焊缝腐蚀质量"};
        private readonly string[] txt2_3 = { "日常监控", "日常监控", "加强管线监测，采取必要的管道减震措施", "加强管线监测；采取管道减震措施，注意操作压力，必要时及时更换管道", "加强管线监测；采取管道减震措施，注意操作压力，必要时及时更换管道" };
        private readonly string txt3_1 = "设计人员做好培训与考核；设计之前对所建管道在设计中可能存在的隐患进行预先性分析；设计工作与第三方监督须做好每日工作记录，便于审核";
        private readonly string txt3_2 = "施工人员做好培训与考核；施工之前对所敷设管道在施工过程中可能存在的隐患进行预先性分析；施工工作与第三方监督须做好每日工作记录，便于审核";
        private readonly string txt3_3 = "制定完善的操作规程与规章制度；操作工人要定期进行培训与考核；做好每日工作日志记录，便于审核";
        private readonly string txt3_4 = "制定合理的维护规程并通过管理部门审核；严格定期进行设备维护与隐患排查；实际维修要参考专家意见；管理部门做好监督管理";
        private readonly string txt3_5 = "编制相应的应急预案，力争预案的全面性与合理性；应急人员要进行安全培训，具备安全意识和一定的安全技术知识；管理部门要做好监督管理工作，确保安全响应";
        private readonly string[] txt4_1 = { "日常监控", "日常监控", "审核管线资料记录，补全管道沿线标记；加强周边环境监控", "审核管线资料记录，补全管道沿线标记；加强周边环境监控", "审核管线资料记录，补全管道沿线标记；加强周边环境监控" };
        private readonly string[] txt4_2 = { "日常监控", "日常监控",
            "巡线人员加强日常巡查，对施工地点重点巡查；管道方选派专职人员对施工进行全过程监督，并督促施工方对全员进行管道知识与后果的培训",
            "巡线人员加强日常巡查，对施工地点重点巡查；管道方选派专职人员对施工进行全过程监督，并督促施工方对全员进行管道知识与后果的培训",
            "巡线人员加强日常巡查，对施工地点重点巡查；管道方选派专职人员对施工进行全过程监督，并督促施工方对全员进行管道知识与后果的培训" };
        private readonly string[] txt4_3 = { "日常监控", "加强管线监测", "加强管线监测", "埋于车行道下，不易受行人故意破坏，但车辆行驶产生的震动以及道路", "施工容易损坏埋地较浅的管线，要加强日常监控，增大管道涂层厚度" };
        private readonly string[] txt4_4 = { "日常监控", "日常监控", "加强管线监测", "埋于非车行道下，易受行人等故意破坏，根据可行性加深管线埋深，若无法施工，需特别加强管线监控", "埋于非车行道下，易受行人等故意破坏，根据可行性加深管线埋深，若无法施工，需特别加强管线监控" };
        private readonly string[] txt4_5 = { "日常监控", "日常监控", "做好日常监控，做好管线标记",
            "老旧管道要重点监测，做好线路标记；新敷设油气管道尽量避开市中心位置，若无法避开，要采取管道加强保护措施，并做好线路标记",
            "老旧管道要重点监测，做好线路标记；新敷设油气管道尽量避开市中心位置，若无法避开，要采取管道加强保护措施，并做好线路标记" };
        private readonly double wA = 0.4019;
        private readonly double wB = 0.1746;
        private readonly double wC = 0.0987;
        private readonly double wD = 0.3248;

        private Score[] scores1;
        private Score[] scores2;
        private Score[] scores3;
        private Score[] scores4;
        private Score[] scores;

        public Form_menu_2()
        {
            InitializeComponent();

            scores = new Score[4];
            scores1 = new Score[5];
            scores2 = new Score[3];
            scores3 = new Score[5];
            scores4 = new Score[5];
        }

        private void Form_menu_2_Load(object sender, EventArgs e)
        {
            textBox40.Text = GlobalData.default_pipeline.Id.ToString();
            textBox38.Text = GlobalData.default_pipeline.Name;
            textBox32.Text = GlobalData.default_pipeline.StartLocation;
            textBox34.Text = GlobalData.default_pipeline.EndLocation;

            textBox42.Text = wA.ToString();
            textBox62.Text = wB.ToString();
            textBox64.Text = wC.ToString();
            textBox66.Text = wD.ToString();
        }

        private void comboBox16_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox16.SelectedIndex;
            textBox52.BackColor = degree_color[index];
            textBox51.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox50.Text = txt1_1[index];
            scores1[0] = (new Score(Score[index], Score[index] + 2, 0.3142));
        }

        private void comboBox17_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox17.SelectedIndex;
            textBox53.BackColor = degree_color[index];
            textBox57.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox49.Text = txt1_2[index];
            scores1[1] = (new Score(Score[index], Score[index] + 2, 0.2224));
        }

        private void comboBox18_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox18.SelectedIndex;
            textBox54.BackColor = degree_color[index];
            textBox58.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox48.Text = txt1_3[index];
            scores1[2] = (new Score(Score[index], Score[index] + 2, 0.1110));
        }

        private void comboBox19_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox19.SelectedIndex;
            textBox55.BackColor = degree_color[index];
            textBox59.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox47.Text = txt1_4[index];
            scores1[3] = (new Score(Score[index], Score[index] + 2, 0.2647));
        }

        private void comboBox20_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox20.SelectedIndex;
            textBox56.BackColor = degree_color[index];
            textBox60.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox46.Text = txt1_5[index];
            scores1[4] = (new Score(Score[index], Score[index] + 2, 0.0877));
        }

        private void comboBox11_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox11.SelectedIndex;
            textBox37.BackColor = degree_color[index];
            textBox36.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox35.Text = txt2_1[index];
            scores2[0] = (new Score(Score[index], Score[index] + 2, 0.3430));
        }

        private void comboBox13_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox13.SelectedIndex;
            textBox39.BackColor = degree_color[index];
            textBox43.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox33.Text = txt2_2[index];
            scores2[1] = (new Score(Score[index], Score[index] + 2, 0.4571));
        }

        private void comboBox15_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox15.SelectedIndex;
            textBox41.BackColor = degree_color[index];
            textBox45.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox31.Text = txt2_3[index];
            scores2[2] = (new Score(Score[index], Score[index] + 2, 0.1999));
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox6.SelectedIndex;
            textBox22.BackColor = degree_color[index];
            textBox21.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox20.Text = txt3_1;
            scores3[0] = (new Score(Score[index], Score[index] + 2, 0.0874));
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox7.SelectedIndex;
            textBox23.BackColor = degree_color[index];
            textBox27.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox19.Text = txt3_2;
            scores3[1] = (new Score(Score[index], Score[index] + 2, 0.1195));
        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox8.SelectedIndex;
            textBox24.BackColor = degree_color[index];
            textBox28.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox18.Text = txt3_3;
            scores3[2] = (new Score(Score[index], Score[index] + 2, 0.1768));
        }

        private void comboBox9_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox9.SelectedIndex;
            textBox25.BackColor = degree_color[index];
            textBox29.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox17.Text = txt3_4;
            scores3[3] = (new Score(Score[index], Score[index] + 2, 0.2590));
        }

        private void comboBox10_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox10.SelectedIndex;
            textBox26.BackColor = degree_color[index];
            textBox30.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox16.Text = txt3_5;
            scores3[4] = (new Score(Score[index], Score[index] + 2, 0.3574));
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            textBox1.BackColor = degree_color[index];
            textBox6.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox11.Text = txt4_1[index];
            scores4[0] = (new Score(Score[index], Score[index] + 2, 0.1185));
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox2.SelectedIndex;
            textBox2.BackColor = degree_color[index];
            textBox7.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox12.Text = txt4_2[index];
            scores4[1] = (new Score(Score[index], Score[index] + 2, 0.3574));
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox3.SelectedIndex;
            textBox3.BackColor = degree_color[index];
            textBox8.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox13.Text = txt4_3[index];
            scores4[2] = (new Score(Score[index], Score[index] + 2, 0.2590));
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox4.SelectedIndex;
            textBox4.BackColor = degree_color[index];
            textBox9.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox14.Text = txt4_4[index];
            scores4[3] = (new Score(Score[index], Score[index] + 2, 0.1768));
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox5.SelectedIndex;
            textBox5.BackColor = degree_color[index];
            textBox10.Text = Util.ParseScore(Score[index], Score[index] + 2);
            textBox15.Text = txt4_5[index];
            scores4[4] = (new Score(Score[index], Score[index] + 2, 0.0873));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double left = 0;
                double right = 0;

                foreach (Score score in scores1)
                {
                    left += score.left * score.weight;
                    right += score.right * score.weight;
                }

                Score new_score = new Score((int)Math.Ceiling(left), (int)Math.Ceiling(right), 0.4019);
                scores[0] = (new_score);

                textBox44.Text = new_score.ToString();
            }
            catch (System.NullReferenceException)
            {
                textBox44.Text = "错误";
                MessageBox.Show("请先选择对应因子的所有评价标准");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double left = 0;
                double right = 0;

                foreach (Score score in scores2)
                {
                    left += score.left * score.weight;
                    right += score.right * score.weight;
                }

                Score new_score = new Score((int)Math.Ceiling(left), (int)Math.Ceiling(right), 0.1746);
                scores[1] = (new_score);

                textBox61.Text = new_score.ToString();
            }
            catch (System.NullReferenceException)
            {
                textBox61.Text = "错误";
                MessageBox.Show("请先选择对应因子的所有评价标准");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                double left = 0;
                double right = 0;

                foreach (Score score in scores3)
                {
                    left += score.left * score.weight;
                    right += score.right * score.weight;
                }

                Score new_score = new Score((int)Math.Ceiling(left), (int)Math.Ceiling(right), 0.0987);
                scores[2] = (new_score);

                textBox63.Text = new_score.ToString();
            }
            catch (System.NullReferenceException)
            {
                textBox63.Text = "错误";
                MessageBox.Show("请先选择对应因子的所有评价标准");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                double left = 0;
                double right = 0;

                foreach (Score score in scores4)
                {
                    left += score.left * score.weight;
                    right += score.right * score.weight;
                }

                Score new_score = new Score((int)Math.Ceiling(left), (int)Math.Ceiling(right), 0.3248);
                scores[3] = (new_score);

                textBox65.Text = new_score.ToString();
            }
            catch (System.NullReferenceException)
            {
                textBox65.Text = "错误";
                MessageBox.Show("请先选择对应因子的所有评价标准");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                double left = 0;
                double right = 0;

                foreach (Score score in scores)
                {
                    left += score.left * score.weight;
                    right += score.right * score.weight;
                }

                Score new_score = new Score((int)Math.Ceiling(left), (int)Math.Ceiling(right), 0.0);
                textBox68.Text = new_score.ToString();
                int _score = new_score.left * new_score.right;
                if (_score > 40)
                {
                    textBox67.Text = "低风险区";
                    textBox70.BackColor = Color.Blue;
                    textBox69.Text = "无警";
                    richTextBox1.Text = "无需采取预警和控制措施";
                }
                else if (_score <= 40 && _score > 30)
                {
                    textBox67.Text = "较低风险区";
                    textBox70.BackColor = Color.Green;
                    textBox69.Text = "轻警";
                    richTextBox1.Text = "无需采取预警和控制措施，但需保存记录";
                }
                else if (_score <= 30 && _score > 20)
                {
                    textBox67.Text = "中等风险区";
                    textBox70.BackColor = Color.Yellow;
                    textBox69.Text = "中警";
                    richTextBox1.Text = "可考虑建立操作规程，加强培训及沟通。采取措施降低风险，直到当降低风险不可行或者降低风险所需成本远远大于收益时方可接收。";
                }
                else if (_score <= 20 && _score > 10)
                {
                    textBox67.Text = "较高风险区";
                    textBox70.BackColor = Color.Orange;
                    textBox69.Text = "重警";
                    richTextBox1.Text = "由企业单位采取措施降低风险，建立运行控制程序，对警报措施进行评价。需要采取有效的措施降低风险至最低合理可接受范围。";
                }
                else if (_score <= 10 && _score > 0) 
                {
                    textBox67.Text = "高风险区";
                    textBox70.BackColor = Color.Red;
                    textBox69.Text = "巨警";
                    richTextBox1.Text = "采取紧急措施降低风险，建立运行控制程序，对警报措施进行评价，必须采取措施降低风险至最低合理可接受；定期向政府主管部门备案。";
                }
                else
                {
                    textBox67.Text = "错误";
                    textBox70.BackColor = Color.Gray;
                    textBox69.Text = "错误";
                    richTextBox1.Text = "错误";
                }
            }
            catch(System.NullReferenceException)
            {
                textBox68.Text = "错误";
                MessageBox.Show("请先计算所有因子的评分");
            }
        }
    }
}
